package com.fengmi.mapper;

import com.fengmi.entity.SysUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Author 123
 * @Date 2022/2/10 11:23
 * @Version 1.0
 */

public interface SysUserMapper {

    //查所有
    @Select("select * from sys_user")
    List<SysUser> findAll();

    //条件查询所有用户信息
    List<SysUser> findUserByWhere(SysUser sysUser);

    //保存用户添加
    @Insert("insert into sys_user (user_name,account,password,remark,user_type,enabled,phone,create_time)" +
            "values (#{userName},#{account},#{password},#{remark},#{userType},#{enabled},#{phone},#{createTime})")
    public int saveAddUserInfo(SysUser sysUser);

    //修改用户的启用状态
    @Update("update sys_user set enabled = #{enabled} where user_id = #{userId}")
    public int updateEnabled(String userId,Integer enabled);

    //修改用户信息
    public int updateUserInfo(SysUser sysUser);

    //通过用户id查询用户的角色id
    @Select("select role_id from sys_user_role where user_id = #{userId}")
    public List<String> findUserRolesByUserId(String userId);

    //给用户分配角色
    public int allocRolesToUser(String userId, String[] roleIds);

    // 删除用户的所有角色
    @Delete("delete from sys_user_role where user_id = #{userId}")
    public int deleteAllRolesById(String userId);

    // 通过id删除用户信息
    @Delete("delete from sys_user where user_id = #{userId}")
    public int deleteUserById(String userId);

    // 通过用户名查询用户
    @Select("select * from sys_user where user_name = #{userName}")
    public SysUser findUserByUserName(String userName);

    //查询用户的角色信息
    @Select("SELECT role.role_name FROM sys_user_role sur " +
            "LEFT JOIN sys_role role ON sur.role_id = role.role_code " +
            "WHERE sur.user_id = #{userId}")
    public List<String> findRolesByUserId(String userId);

    // 查询用户权限信息
    @Select("SELECT per.perm_name FROM sys_user_role sur \n" +
            "LEFT JOIN sys_role_permission srp ON sur.role_id = srp.role_id\n" +
            "LEFT JOIN sys_permission per ON srp.perm_id = per.perm_code\n" +
            "WHERE sur.user_id = 1;")
    public List<String> findPermissionByUserId(Integer userId);
}
